package com.jielihaofeng.chat.controller;

import com.jielihaofeng.chat.convert.ChatUserRelationConvert;
import com.jielihaofeng.chat.entity.ChatUserRelationEntity;
import com.jielihaofeng.chat.query.ChatUserRelationQuery;
import com.jielihaofeng.chat.service.ChatUserRelationService;
import com.jielihaofeng.chat.vo.ChatUserRelationVO;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.AllArgsConstructor;
import com.jielihaofeng.framework.common.page.PageResult;
import com.jielihaofeng.framework.common.utils.Result;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
* 用户好友表
*
* @author 借力好风 949394048@qq.com
* @since 1.0.0 2022-07-22
*/
@RestController
@RequestMapping("chat/chatuserrelation")
@Tag(name="用户好友表")
@AllArgsConstructor
public class ChatUserRelationController {
    private final ChatUserRelationService chatUserRelationService;

    @GetMapping("page")
    @Operation(summary = "分页")
    @PreAuthorize("hasAuthority('chat:chatuserrelation:page')")
    public Result<PageResult<ChatUserRelationVO>> page(@Valid ChatUserRelationQuery query){
        PageResult<ChatUserRelationVO> page = chatUserRelationService.page(query);

        return Result.ok(page);
    }

    @GetMapping("{id}")
    @Operation(summary = "信息")
    @PreAuthorize("hasAuthority('chat:chatuserrelation:info')")
    public Result<ChatUserRelationVO> get(@PathVariable("id") Long id){
        ChatUserRelationEntity entity = chatUserRelationService.getById(id);

        return Result.ok(ChatUserRelationConvert.INSTANCE.convert(entity));
    }

    @PostMapping
    @Operation(summary = "保存")
    @PreAuthorize("hasAuthority('chat:chatuserrelation:save')")
    public Result<String> save(@RequestBody ChatUserRelationVO vo){
        chatUserRelationService.save(vo);

        return Result.ok();
    }

    @PutMapping
    @Operation(summary = "修改")
    @PreAuthorize("hasAuthority('chat:chatuserrelation:update')")
    public Result<String> update(@RequestBody @Valid ChatUserRelationVO vo){
        chatUserRelationService.update(vo);

        return Result.ok();
    }

    @DeleteMapping
    @Operation(summary = "删除")
    @PreAuthorize("hasAuthority('chat:chatuserrelation:delete')")
    public Result<String> delete(@RequestBody List<Long> idList){
        chatUserRelationService.delete(idList);

        return Result.ok();
    }


}